package com.wang.back.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSONObject;
import com.wang.common.pojo.InfoEnum;
import com.wang.common.servlet.BaseServlet;
import com.wang.common.util.ConstatFinalUtil;
import com.wang.common.util.PageInfoUtil;
import com.wang.pojo.AAdmins;
import com.wang.service.IUsersDbService;
import com.wang.service.impl.UsersDbServiceImpl;

/**
 * 第二个Servlet 只存储和管理员息息相关的信息 登陆后的操作
 * 
 * @author wangshMac
 */
@WebServlet(urlPatterns = "/back/AdminsBackServlet")
public class AdminsBackServlet extends BaseServlet
{
	private IUsersDbService usersDbService = new UsersDbServiceImpl();
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 接收参数 */
		String method = request.getParameter("method");
		if ("main".equalsIgnoreCase(method))
		{
			/* 登陆后首页 */
			main(request, response);
		} else if ("welcome".equalsIgnoreCase(method))
		{
			/* 欢迎页面 */
			welcome(request, response);
		} else if ("logout".equalsIgnoreCase(method))
		{
			/* 退出 */
			logout(request, response);
		}else if("adminsList".equalsIgnoreCase(method))
		{
			/* 管理员列表 */
			adminsList(request,response);
		}else if("adminsInsert".equalsIgnoreCase(method))
		{
			/* 管理员添加页面 */
			adminsInsert(request,response);
		}else if("adminsInsertSubmit".equalsIgnoreCase(method))
		{
			/* 管理员添加提交操作 */
			adminsInsertSubmit(request,response);
		}else if("adminsUpdate".equalsIgnoreCase(method))
		{
			/* 管理员更新页面 */
			adminsUpdate(request,response);
		}else if("adminsUpdateSubmit".equalsIgnoreCase(method))
		{
			/* 管理员更新提交操作 */
			adminsUpdateSubmit(request,response);
		}
	}

	/**
	 * 退出
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void logout(HttpServletRequest request, HttpServletResponse response) throws IOException
	{
		HttpSession session = request.getSession();
		/* 从session中移除 */
		String ip = request.getRemoteAddr() ; 
		AAdmins admins = (AAdmins) session.getAttribute("admins");
		session.removeAttribute("lastLoginTime");
		session.removeAttribute("admins");
		
		/* 说明已经登陆 */
		response.sendRedirect(request.getContextPath() + "/NoLoginServlet?method=adminsLogin");
	}

	/**
	 * 欢迎页面
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void welcome(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 准备路径 */
		this.path = "/back/admins/welcome";
		/* 页面跳转 */
		this.serverRedirect(request, response, path);
	}

	/**
	 * 首页
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void main(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 准备路径 */
		this.path = "/back/admins/main";
		/* 页面跳转 */
		this.serverRedirect(request, response, path);
	}
	
	/**
	 * 管理员列表
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void adminsList(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException
	{
		System.out.println("==AdminsBackServlet==adminsList");
		/* 分页工具类 */
		PageInfoUtil pageInfoUtil = proccedPageInfo(request);
		/* 处理搜索条件 */
		Map<String, Object> condMap = proccedSearch(request);
		
		/* ==性别处理开始== */
		String sex = request.getParameter("sex");
		if(sex == null)
		{
			sex = "" ; 
		}
		condMap.put("sex", sex);
		request.setAttribute("sex", sex);
		/* ==性别处理结束== */
		
		List<AAdmins> adminsList = this.usersDbService.findCondListAdminsService(pageInfoUtil,condMap);
		/* 存储四大作用域 */
		request.setAttribute("list", adminsList);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		
		/* 服务端跳转页面 */
		this.serverRedirect(request, response, "/back/admins/adminsList");
	}
	
	/* 管理员添加页面 */
	private void  adminsInsert(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException
	{
		System.out.println("==adminsInsert===");
		Map<String,Object> condMap = new HashMap<String, Object>();
		/* 查询所有状态为启用的角色(不能分页);前题我们得清楚记录不是很多
		 * 应该是角色的枚举(模仿着管理员的枚举写一个角色的枚举呢???)
		 *  */
		condMap.clear();
		condMap.put("status", "1");
		/* 直接跳转到管理员添加页面中 */
		this.serverRedirect(request, response, "/back/admins/adminsInsert");
	}
	
	/**
	 * 管理员添加提交操作
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void adminsInsertSubmit(HttpServletRequest request, HttpServletResponse response) throws IOException
	{
		System.out.println("==adminsInsertSubmit===");
		/* 接收参数 */
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		String trueName = request.getParameter("trueName");
		String qq = request.getParameter("qq");
		String phone = request.getParameter("phone");
		/* sex:接收的时候是字符串,但是在pojo中byte类型;处理参数的目标对象 */
		String sex = request.getParameter("sex");
		String status = request.getParameter("status");
		/* 处理参数 */
		byte sexByte = Byte.parseByte(sex);
		byte statusByte = Byte.parseByte(status);
		/* new对象 */
		AAdmins admins = new AAdmins();
		/* 为对象赋值 */
		admins.setEmail(email);
		admins.setPassword(password);
		admins.setTrueName(trueName);
		admins.setQq(qq);
		admins.setPhone(phone);
		admins.setSex(sexByte);
		admins.setStatus(statusByte);
		
		
		/* 调用Service方法 */
		JSONObject resultJSON = this.usersDbService.saveOneAdminsService(admins);
		/* 存储Service方法执行结果,目的是为了要把结果通知给jsp */
		/*  响应是json */
		this.printStr(request, response, resultJSON.toJSONString());
	}
	
	/**
	 * 打开管理员更新页面
	 * 		修改个人信息(不包含密码);method=adminsUpdate(需要修改service方法)
	 * 		重置密码;method=adminsResetPass
	 * 		查看个人信息,不修改;method=adminsInfo
	 * 
	 * 额外增加一个参数:operType(这种方法)
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void adminsUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		System.out.println("==adminsUpdate===");
		/* 根据id查询管理员 */
		String id = request.getParameter("id");
		Map<String,Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		AAdmins admins = this.usersDbService.findOneAdminsService(condMap);
		/* 我们要跳转页面,所以把admins信息放到request中;因为要在页面上显示 */
		request.setAttribute("admins", admins);
		
		/* 路径 */
		String path = "" ; 
		/* 接收参数,做区分 */
		String operType = request.getParameter("operType");
		if("adminsInfo".equalsIgnoreCase(operType))
		{
			/* 查看个人信息页面 */
			path = "/back/admins/adminsInfo" ; 
		}else if("adminsUpdate".equalsIgnoreCase(operType))
		{
			/* 打开更新个人信息页面 */
			path = "/back/admins/adminsUpdate" ; 
			/* 查询所有状态为启用的角色(不能分页);前题我们得清楚记录不是很多
			 * 应该是角色的枚举(模仿着管理员的枚举写一个角色的枚举呢???)
			 *  */
			condMap.clear();
			condMap.put("status", "1");
		}else if("adminsResetPass".equalsIgnoreCase(operType))
		{
			/* 重置密码 */
			path = "/back/admins/adminsResetPass" ;
		}
	
		this.serverRedirect(request, response, path);
	}

	/**
	 * 更新提交操作
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void adminsUpdateSubmit(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
	{
		System.out.println("==adminsUpdate===");
		/* 根据id查询管理员 */
		String id = request.getParameter("id");
		Map<String,Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		condMap.put("extend","true");
		AAdmins admins = this.usersDbService.findOneAdminsService(condMap);
		/* 我们要跳转页面,所以把admins信息放到request中;因为要在页面上显示 */
		request.setAttribute("admins", admins);
		
		boolean updateFlag = false ;
		/* 接收参数,做区分 */
		String operType = request.getParameter("operType");
		if("adminsUpdate".equalsIgnoreCase(operType))
		{
			// 修改个人信息
			/* 接收参数 */
			String email = request.getParameter("email");
			String trueName = request.getParameter("trueName");
			String qq = request.getParameter("qq");
			String phone = request.getParameter("phone");
			/* sex:接收的时候是字符串,但是在pojo中byte类型;处理参数的目标对象 */
			String sex = request.getParameter("sex");
			String status = request.getParameter("status");
			/* 处理参数 */;
			byte sexByte = Byte.parseByte(sex);
			byte statusByte = Byte.parseByte(status);
			/* 为对象赋值 */
			admins.setEmail(email);
			admins.setTrueName(trueName);
			admins.setQq(qq);
			admins.setPhone(phone);
			admins.setSex(sexByte);
			admins.setStatus(statusByte);
			
			
			updateFlag = true ; 
		}else if("adminsResetPass".equalsIgnoreCase(operType))
		{
			// 重置密码
			String password = request.getParameter("password");
			/* 密码加密 */
			admins.setPassword(this.enuEncryptUtil.encodeStr(password));
			updateFlag = true ;  
		}
		
		JSONObject resultJSON = new JSONObject();
		/* 更新数据库 */
		if(updateFlag)
		{
			/* 要更新数据库 */
			resultJSON = this.usersDbService.updateOneAdminsService(admins);
		}else
		{
			/* 操作失败 */
			resultJSON.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_OPER_ERROR.getCode());
			resultJSON.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.getString(InfoEnum.INFO_OPER_ERROR.getCode() + ""));
		}
		
		this.printStr(request, response, resultJSON.toJSONString());
	}
	
}
